package com.lc.zpyh.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.base.BaseDialog;
import com.lc.zpyh.R;
import com.lc.zpyh.app.TitleBarFragment;
import com.lc.zpyh.http.glide.GlideApp;
import com.lc.zpyh.http.request.FavoritesListApi;
import com.lc.zpyh.http.request.UserByidApi;
import com.lc.zpyh.http.request.UserCouponQueryApi;
import com.lc.zpyh.http.request.UserWatchListApi;
import com.lc.zpyh.http.response.FavoritesListBean;
import com.lc.zpyh.http.response.UserByidBean;
import com.lc.zpyh.http.response.UserCouponQueryBean;
import com.lc.zpyh.http.response.UserWatchListBean;
import com.lc.zpyh.manager.ActivityManager;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.ui.activity.BindMobilePhoneNumberActivity;
import com.lc.zpyh.ui.activity.MainActivity;
import com.lc.zpyh.ui.activity.mine.CollectionActivity;
import com.lc.zpyh.ui.activity.mine.CooperateActivity;
import com.lc.zpyh.ui.activity.mine.CouponActivity;
import com.lc.zpyh.ui.activity.mine.FeedbackActivity;
import com.lc.zpyh.ui.activity.mine.FootPrintActivity;
import com.lc.zpyh.ui.activity.mine.LoginCodeActivity;
import com.lc.zpyh.ui.activity.mine.MyEvaluateActivity;
import com.lc.zpyh.ui.activity.mine.PersonalCenterActivity;
import com.lc.zpyh.ui.activity.mine.ShopFollowActivity;
import com.lc.zpyh.ui.activity.mine.SystemActivity;
import com.lc.zpyh.ui.dialog.MessageDialog;
import com.lc.zpyh.util.CourseUtils;
import com.lc.zpyh.util.SPUtil;
import com.lc.zpyh.util.constant.EvenConstant;
import com.lc.zpyh.util.eventbus.Event;
import com.lc.zpyh.util.eventbus.EventBusUtils;
import com.lc.zpyh.widget.UIHelper;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MineFragment extends TitleBarFragment<MainActivity> {
    private static final String TAG = "MineFragment";
    @BindView(R.id.my_pic)
    ImageView myPic;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_login)
    TextView tvLogin;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_app_out)
    TextView tvAppOut;
    @BindView(R.id.tv_follow)
    TextView tvFollow;
    @BindView(R.id.tv_pack_volume)
    TextView tvPackVolume;
    @BindView(R.id.tv_collect)
    TextView tvCollect;

    private List<UserWatchListBean.ListBean> userWatchList=new ArrayList<>();
    private List<UserCouponQueryBean.ListBean> userCouponList=new ArrayList<>();
    private List<FavoritesListBean.ListBean> favoritesList=new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onStart() {
        super.onStart();

//

        if ("".equals(SPUtil.get(getContext(), IntentKey.USERID, "")) || SPUtil.get(getContext(), IntentKey.USERID, "") == null || SPUtil.get(getContext(), IntentKey.USERID, "").isEmpty()) {
            tvName.setVisibility(View.GONE);
            tvPhone.setVisibility(View.GONE);
            tvLogin.setVisibility(View.VISIBLE);
            tvAppOut.setVisibility(View.GONE);
            tvFollow.setText("0");
            tvPackVolume.setText("0");
            tvCollect.setText("0");
        }else {
            tvName.setVisibility(View.VISIBLE);
            tvPhone.setVisibility(View.VISIBLE);
            tvLogin.setVisibility(View.GONE);
            tvAppOut.setVisibility(View.VISIBLE);
//            个人信息
            getAccount();
//  关注
            userWatchList();
//          优惠券
            userCouponQuery();
//            我的收藏列表
            favoritesList();
        }
        }
    /**
     * 我的收藏列表
     */
    private void favoritesList() {
        EasyHttp.post(this)
                .api(new FavoritesListApi().setUserId(SPUtil.get(getContext(), IntentKey.USERID, "")).setPageNum("1").setPageSize("999")).request(new HttpCallback<FavoritesListBean>(this) {

            @Override
            public void onSucceed(FavoritesListBean data) {
                if (data.getCode()==0) {
                    tvCollect.setText(data.getList().size()+"");
                    favoritesList.clear();
                    favoritesList.addAll(data.getList());
                }

            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    /**
     * 优惠券
     */
    private void userCouponQuery() {
        EasyHttp.post(this)
                .api(new UserCouponQueryApi().setUserId(SPUtil.get(getContext(), IntentKey.USERID, "")).setType("0").setPageNum("1").setPageSize("100")).request(new HttpCallback<UserCouponQueryBean>(this) {

            @Override
            public void onSucceed(UserCouponQueryBean data) {
                if (data.getCode()==0) {
                    tvPackVolume.setText(data.getList().size()+"");
                    userCouponList.clear();
                    userCouponList.addAll(data.getList());
                }

            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    /**
     * 关注
     */
    private void userWatchList() {
        EasyHttp.post(this)
                .api(new UserWatchListApi().setUserId(SPUtil.get(getContext(), IntentKey.USERID, ""))).request(new HttpCallback<UserWatchListBean>(this) {

            @Override
            public void onSucceed(UserWatchListBean data) {
                if (data.getCode()==0) {
                    tvFollow.setText(data.getList().size()+"");
                    userWatchList.clear();
                    userWatchList.addAll(data.getList());
                }

            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void getAccount() {
        EasyHttp.post(this)
                .api(new UserByidApi().setUserId(SPUtil.get(getContext(), IntentKey.USERID, ""))).request(new HttpCallback<UserByidBean>(this) {

            @Override
            public void onSucceed(UserByidBean data) {
                if (data.getCode()==0) {
                    try {


                    GlideApp.with(getActivity())
                            .load(IntentKey.CAR_IMAGE+data.getList().get(0).getUserImg())
                            .placeholder(R.drawable.avatar_placeholder_ic)
                            .error(R.drawable.avatar_placeholder_ic)
                            .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                            .into(myPic);
                    if (data.getList().get(0).getUserName()!=null) {

                        if (data.getList().get(0).getUserName().isEmpty()) {
                            tvName.setText("点击设置昵称");
                        } else {
                            tvName.setText(data.getList().get(0).getUserName());
                        }
                    }else {
                        tvName.setText("点击设置昵称");
                    }
                    if (data.getList().get(0).getUserPhone()!=null) {

                        if (data.getList().get(0).getUserPhone().isEmpty()) {
                            tvPhone.setText("点击绑定手机");
                        } else {
                            tvPhone.setText(data.getList().get(0).getUserPhone());
                            SPUtil.put(getContext(),IntentKey.PHONE,data.getList().get(0).getUserPhone());
                        }
                    }else {
                        tvPhone.setText("点击绑定手机");
                    }

                   SPUtil.put(getContext(),IntentKey.NICKNAME,data.getList().get(0).getUserName());
                    SPUtil.put(getContext(),IntentKey.PHONE,data.getList().get(0).getUserPhone());
                    SPUtil.put(getContext(),IntentKey.HEADIMGURL,data.getList().get(0).getUserImg());
                    }catch (Exception e){

                    }
                }

            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }


    @OnClick({R.id.my_pic, R.id.ll_my_fukuan, R.id.ll_my_canyin, R.id.ll_my_yunshu, R.id.ll_my_pingjia, R.id.ll_my_shouhou, R.id.ll_mine_hezuo, R.id.ll_mine_xitongshezhi, R.id.ll_mine_pingjia, R.id.ll_mine_liwu, R.id.ll_mine_fankui, R.id.tv_app_out,R.id.tv_all_qiangdan,R.id.ll_coupon,R.id.ll_shoucang,R.id.ll_guanzhu,R.id.tv_name,R.id.tv_login,R.id.tv_phone})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_pic:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginCodeActivity.class);
                    return;
                }
                UIHelper.startActivity(getContext(), PersonalCenterActivity.class);
                break;
            case R.id.tv_login:
                startActivity(LoginCodeActivity.class);
                break;
            case R.id.tv_name:
                UIHelper.startActivity(getContext(), PersonalCenterActivity.class);
                break;
            case R.id.tv_phone:
                startActivity(new Intent(getContext(), BindMobilePhoneNumberActivity.class).putExtra("type","qq"));
                break;
            case R.id.ll_coupon:

                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginCodeActivity.class);
                    return;
                }
                UIHelper.startActivity(getContext(), CouponActivity.class);
                break;
            case R.id.ll_guanzhu:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginCodeActivity.class);
                    return;
                }
                startActivity(new Intent(getContext(), ShopFollowActivity.class).putExtra("userWatchList", (Serializable) userWatchList));


                break;
            case R.id.ll_shoucang:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginCodeActivity.class);
                    return;
                }
                startActivity(new Intent(getContext(), CollectionActivity.class).putExtra("favoritesList", (Serializable) favoritesList));

                break;
            case R.id.ll_my_fukuan:
                EventBusUtils.sendEvent(new Event(EvenConstant.TABONE, 1));

                break;
            case R.id.ll_my_canyin:
                EventBusUtils.sendEvent(new Event(EvenConstant.TABTWO, 2));
                break;
            case R.id.ll_my_yunshu:
                EventBusUtils.sendEvent(new Event(EvenConstant.TABTHREE, 3));
                break;
            case R.id.ll_my_pingjia:
                EventBusUtils.sendEvent(new Event(EvenConstant.TABFOUR, 4));
                break;
            case R.id.ll_my_shouhou:
                EventBusUtils.sendEvent(new Event(EvenConstant.TABFIVE, 5));
                break;
            case R.id.tv_all_qiangdan:

                EventBusUtils.sendEvent(new Event(EvenConstant.TABALL, 0));
                break;
            case R.id.ll_mine_hezuo:
                UIHelper.startActivity(getContext(), CooperateActivity.class);
                break;
            case R.id.ll_mine_xitongshezhi:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginCodeActivity.class);
                    return;
                }
                UIHelper.startActivity(getContext(), SystemActivity.class);

                break;
            case R.id.ll_mine_pingjia:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginCodeActivity.class);
                    return;
                }
//                我的评价
                UIHelper.startActivity(getContext(), MyEvaluateActivity.class);
                break;
            case R.id.ll_mine_liwu:
                UIHelper.startActivity(getContext(), FootPrintActivity.class);

                break;
            case R.id.ll_mine_fankui:
                if (!CourseUtils.isNotNullOrEmpty(SPUtil.get(getContext(),IntentKey.USERID,""))){
                    startActivity(LoginCodeActivity.class);
                    return;
                }
                UIHelper.startActivity(getContext(), FeedbackActivity.class);

                break;
            case R.id.tv_app_out:
                // 消息对话框
                new MessageDialog.Builder(getActivity())
                        // 标题可以不用填写
                        .setTitle("退出登录")
                        // 内容必须要填写
                        .setMessage("您是否要退出登录？")
                        // 确定按钮文本
                        .setConfirm(getString(R.string.common_confirm))
                        // 设置 null 表示不显示取消按钮
                        .setCancel(getString(R.string.common_cancel))
                        // 设置点击按钮后不关闭对话框
                        //.setAutoDismiss(false)
                        .setListener(new MessageDialog.OnListener() {

                            @Override
                            public void onConfirm(BaseDialog dialog) {
                                // 进行内存优化，销毁除登录页之外的所有界面
                                ActivityManager.getInstance().finishAllActivities(MainActivity.class);
                                startActivity(LoginCodeActivity.class);

                                SPUtil.remove(getContext(),IntentKey.USERID);
                                myPic.setImageResource(R.mipmap.mine_default_avatar);
                            }

                            @Override
                            public void onCancel(BaseDialog dialog) {
                                dialog.dismiss();
                            }
                        })
                        .show();

                break;

        }
    }
}
